package test.patterns;

/** A concrete example of a subject in the Observer pattern.
    Here it is the 'bare bones' of a spreadsheet cell whose
    integer value can be monitored.
 
   This source code is from the book 
     "Flexible, Reliable Software:
       Using Patterns and Agile Development"
     published 2010 by CRC Press.
   Author: 
     Henrik B Christensen 
     Computer Science Department
     Aarhus University
   
   This source code is provided WITHOUT ANY WARRANTY either 
   expressed or implied. You may study, use, modify, and 
   distribute it for non-commercial purposes. For any 
   commercial use, see http://www.baerbak.com/
*/
import java.util.*;

public class ObsSpreadsheetCell implements ObsSubject {
  private int value = 0;
  
  /** change the value of the cell.
      @param value the new value in the cell.
  */
  public void changeValue(int value) { 
    this.value = value; 
    notifyObservers();
  }
  
  /** get the value of the cell.
      @return the value of this cell.
  */
  public int getValue() {
    return value;
  }
  
  private List<ObsObserver> observerList = new ArrayList<ObsObserver>();
  public void addObserver(ObsObserver newObserver) { 
    observerList.add(newObserver);
  }
  public void removeObserver(ObsObserver observer) {
    observerList.remove(observer);
  }
  public void notifyObservers() {
    for( ObsObserver o: observerList ) {
      o.update();
    }
  }
}